package com.leanin.form.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.leanin.form.exception.ExceptionCast;
import com.leanin.form.mapper.LeaninFollowFormDeptMapper;
import com.leanin.form.mapper.LeaninFollowFormMapper;
import com.leanin.form.model.*;
import com.leanin.form.model.request.FollowFormDTO;
import com.leanin.form.model.request.FormQueryDTO;
import com.leanin.form.model.response.*;
import com.leanin.form.model.response.followform.FollowFormCode;
import com.leanin.form.service.FollowFormService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @author CPJ.
 * @date 2019/6/17.
 * @time 9:58.
 */
@Service
public class FollowFormServiceImpl implements FollowFormService {

    @Autowired
    private LeaninFollowFormMapper leaninFollowFormMapper;

    @Autowired
    private LeaninFollowFormDeptMapper leaninFollowFormDeptMapper;


    private Integer getDownLoadCount(){
        Random random = new Random();
        int count = random.nextInt(50);
        return count + 50 ;
    }


    //判断参数是否合理
    private void checkParam(FollowFormDTO followFormDTO) {
        if (followFormDTO.getFormName() == null) {//宣教表单名为空
            ExceptionCast.cast(FollowFormCode.FORM_NAME_NULL);
        }
        if (followFormDTO.getFormName().length() > 50){//宣教表单名称  超过 50 字
            ExceptionCast.cast(FollowFormCode.FORM_NAME_LENGTH_ERROR);
        }
        if (followFormDTO.getFormTitle().length() > 150){//宣教表单头  超过 150 字
            ExceptionCast.cast(FollowFormCode.FORM_TITLE_LENGTH_ERROR);
        }
        if (followFormDTO.getFormEnd().length() > 150){//宣教表单名称  超过 150 字
            ExceptionCast.cast(FollowFormCode.FORM_END_LENGTH_ERROR);
        }
        if (followFormDTO.getFormContent() == null) {//表单内容为空
            ExceptionCast.cast(FollowFormCode.FORM_CONTENT_NULL);
        }


    }
    //添加科室信息
    private void insertDepts(FollowFormDTO followFormDTO){
        List<String> depts = followFormDTO.getDepts();
        if (depts != null && depts.size() > 0) {//判断宣教科室是否为空
            List<LeaninFollowFormDept> params = new ArrayList<>();
            for (String dept : depts) {
                LeaninFollowFormDept leaninFollowFormDept = new LeaninFollowFormDept();
                leaninFollowFormDept.setFollowFormId(followFormDTO.getId());
                leaninFollowFormDept.setDeptCode(dept);
                params.add(leaninFollowFormDept);
            }
            leaninFollowFormDeptMapper.insertList(params);
        }
    }

    @Override
    @Transactional
    public ResponseResult addFollowForm(FollowFormDTO followFormDTO) {
        //判断参数是否合理
        checkParam(followFormDTO);
        List<LeaninFollowForm> leaninFollowForms = leaninFollowFormMapper.selectByFormName(followFormDTO.getFormName());
        if (leaninFollowForms.size() > 0) {//宣教表单名重复
            ExceptionCast.cast(FollowFormCode.FORM_NAME_REPETITION);
        }
        LeaninFollowForm leaninFollowForm =new FollowFormDTO();
        followFormDTO.setFormCreateTime(new Date());
        followFormDTO.setDownloadCount(getDownLoadCount());
        BeanUtils.copyProperties(followFormDTO,leaninFollowForm);
        leaninFollowFormMapper.insertSelective(leaninFollowForm);
        followFormDTO.setId(leaninFollowForm.getId());
        //添加随访表单和科室的关联表
        insertDepts(followFormDTO);
        return new ResponseResult(CommonCode.SUCCESS);
    }

    @Override
    @Transactional
    public ResponseResult updateFollowForm(FollowFormDTO followFormDTO) {
        if (followFormDTO.getId() == null){
            ExceptionCast.cast(FollowFormCode.FORM_NOT_EXIST);
        }
        //检查参数是否合理
        checkParam(followFormDTO);
        List<LeaninFollowForm> leaninFollowForms = leaninFollowFormMapper.selectByFormName(followFormDTO.getFormName());
        if (leaninFollowForms.size() > 1) {//宣教表单名重复
            ExceptionCast.cast(FollowFormCode.FORM_NAME_REPETITION);
        }
        LeaninFollowForm leaninFollowForm = leaninFollowFormMapper.selectByPrimaryKey(followFormDTO.getId());
        if (leaninFollowForm == null){
            ExceptionCast.cast(FollowFormCode.FORM_NOT_EXIST);
        }
        LeaninFollowForm FollowForm = new LeaninFollowForm();
        BeanUtils.copyProperties(followFormDTO,FollowForm);
        FollowForm.setDownloadCount(leaninFollowForm.getDownloadCount());
        FollowForm.setFormCreateTime(leaninFollowForm.getFormCreateTime());
        FollowForm.setFormBelong(leaninFollowForm.getFormBelong());
        leaninFollowFormMapper.updateByPrimaryKeySelective(FollowForm);

        //删除原先绑定的科室
        leaninFollowFormDeptMapper.deleteByFormId(followFormDTO.getId());
        //重新添加科室
        insertDepts(followFormDTO);
        return new ResponseResult(CommonCode.SUCCESS);
    }

    @Override
    public QueryResponseResult findByParam(FormQueryDTO formQueryDTO) {
        Integer page = formQueryDTO.getPage();
        if (page == null || page < 1){
            page = 1;
        }
        Integer pageSize = formQueryDTO.getPageSize();
        if (pageSize == null || pageSize < 0){
            pageSize = 10;
        }
        PageHelper.startPage(page,pageSize);
        Page<FollowFormVO> pageList = (Page<FollowFormVO>) leaninFollowFormMapper.findByParam(formQueryDTO);
        //封装返回结果
        QueryResult<FollowFormVO> queryResult = new QueryResult<>();
        queryResult.setTotal(pageList.getTotal());
        queryResult.setList(pageList.getResult());
        return new QueryResponseResult(CommonCode.SUCCESS,queryResult);
    }

    @Override
    public CommonResponseResult findById(Long formId) {
        FollowFormVO followFormVO = leaninFollowFormMapper.findById(formId);
        if (followFormVO == null){
            ExceptionCast.cast(FollowFormCode.FORM_NOT_EXIST);
        }
        return new CommonResponseResult(CommonCode.SUCCESS,followFormVO);
    }

    @Override
    @Transactional
    public ResponseResult addDownLoadCount(Long formId) {
        LeaninFollowForm leaninFollowForm = leaninFollowFormMapper.selectByPrimaryKey(formId);
        if (leaninFollowForm == null){
            ExceptionCast.cast(FollowFormCode.FORM_NOT_EXIST);
        }
        leaninFollowForm.setDownloadCount(leaninFollowForm.getDownloadCount() + 1);
        leaninFollowFormMapper.updateByPrimaryKeySelective(leaninFollowForm);
        return new ResponseResult(CommonCode.SUCCESS);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public ResponseResult deleteForm(Long formId) {
        LeaninFollowForm leaninFollowForm = leaninFollowFormMapper.selectByPrimaryKey(formId);
        if (leaninFollowForm == null){
            ExceptionCast.cast(FollowFormCode.FORM_NOT_EXIST);
        }
        leaninFollowFormMapper.deleteByPrimaryKey(formId);
        leaninFollowFormDeptMapper.deleteByFormId(formId);
        return ResponseResult.SUCCESS();
    }


}
